package tool;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;




public class TimeTool {
	static String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
	
	
	static public String getNowTime(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		return df.format(new Date());// new Date()为获取当前系统时间
	
	}
	
	
	
	
	//获取系统时间
	static public Timestamp getCurrentTime(){
		return new Timestamp(System.currentTimeMillis());
	}
	
	//构造当月起止时间
	static public Timestamp getMonthStartOrEnd(int type){
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		if(type==1){
			cal.set(Calendar.DATE, 1);
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			return new Timestamp(cal.getTimeInMillis());
		}else if(type==2){
			cal.set(Calendar.MONTH, cal.get(Calendar.MONTH)+1);
			cal.set(Calendar.DATE, 1);
			cal.set(Calendar.HOUR_OF_DAY, 0);
			cal.set(Calendar.MINUTE, 0);
			cal.set(Calendar.SECOND, 0);
			return new Timestamp(cal.getTimeInMillis());
		}
		return null;
	}
	
	
	//获取当日星期几
	static public String getWeekDay(){
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
	}
	
	
	
	static public String getTodayBeginStr(){
		DateFormat myFmt= DateFormat.getDateInstance();
		  Date now=new Date();
	      return myFmt.format(now);
	}
	
	/**返回N天后的时间戳
	 * @param  months:1~12
	 */
	static public Timestamp nDaysLeater(int days){
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, days);
		return new Timestamp(cal.getTimeInMillis());
	}
	

	/**返回N月后的时间戳
	 * @param  months:1~12
	 */
	static public Timestamp nMonthLeater(int months){
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, months);
		return new Timestamp(cal.getTimeInMillis());
	}
	
	/**
	 * @return N 天后的时间戳
	 */
	static public Timestamp nDaysLeter(int days){
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, days);
		return new Timestamp(cal.getTimeInMillis());
	}
	
	//构造 某天的时间戳（2016 2 29  同步数据的时刻   0点第一次同步  1点第二次同步 （实际是3.27第二次同步）） 
	static public Timestamp getSyncTime(){
		 Calendar cal = Calendar.getInstance();
		  
		 cal.set(Calendar.MONTH, 1);
		 cal.set(Calendar.DATE, 28);
		  cal.set(Calendar.HOUR_OF_DAY, 1);
		  cal.set(Calendar.SECOND, 0);
		  cal.set(Calendar.MINUTE, 0);
		  cal.set(Calendar.MILLISECOND, 001);
		  return new Timestamp(cal.getTimeInMillis());
	}
	
	//构造某天的时间戳
	static public Timestamp generateTime(int year,int month,int date){
		 Calendar cal = Calendar.getInstance();
		 cal.set(Calendar.YEAR, year);
		 cal.set(Calendar.MONTH, month-1);
		 cal.set(Calendar.DATE, date);
		  cal.set(Calendar.HOUR_OF_DAY, 0);
		  cal.set(Calendar.SECOND, 0);
		  cal.set(Calendar.MINUTE, 0);
		  cal.set(Calendar.MILLISECOND, 001);
		  return new Timestamp(cal.getTimeInMillis());
	}
	
	
	//构造 某天开始或结束的时间戳（2016 2 29  同步数据的时刻） 
	static public Timestamp getTimeBeginOrEnd(int year,int month,int date,boolean isEnd){
		 Calendar cal = Calendar.getInstance();
		  
		 if(!isEnd){
			 cal.set(Calendar.MONTH, month-1);
			 cal.set(Calendar.DATE, date);
			 cal.set(Calendar.HOUR_OF_DAY, 0);
			 cal.set(Calendar.SECOND, 0);
			 cal.set(Calendar.MINUTE, 0);
			 cal.set(Calendar.MILLISECOND, 001);
		 }else{
			 cal.set(Calendar.MONTH, month-1);
			 cal.set(Calendar.DATE, date);
			 cal.set(Calendar.HOUR_OF_DAY, 23);
			 cal.set(Calendar.SECOND, 59);
			 cal.set(Calendar.MINUTE, 59);
			 cal.set(Calendar.MILLISECOND, 999);
			 
		 }
		  return new Timestamp(cal.getTimeInMillis());
	}
	
	
	
	//构造当天零点零分的时间戳 
	static public Timestamp getTodayBegin(){
		 Calendar cal = Calendar.getInstance();
		  cal.set(Calendar.HOUR_OF_DAY, 0);
		  cal.set(Calendar.SECOND, 0);
		  cal.set(Calendar.MINUTE, 0);
		  cal.set(Calendar.MILLISECOND, 001);
		  return new Timestamp(cal.getTimeInMillis());
	}
	
	//构造当天零点零分的时间戳 
	static public Timestamp getYestodayBegin(){
		 Calendar cal = Calendar.getInstance();
		 
		 cal.add(Calendar.DATE, -1);
		  cal.set(Calendar.HOUR_OF_DAY, 0);
		  cal.set(Calendar.SECOND, 0);
		  cal.set(Calendar.MINUTE, 0);
		  cal.set(Calendar.MILLISECOND, 001);
		  return new Timestamp(cal.getTimeInMillis());
	}
	
	// Timestamp convert data
	static public Date convertDate(Timestamp time){
		return new java.sql.Date(time.getTime()); 
	}
	
	static public Calendar convertCal(Timestamp time){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new java.sql.Date(time.getTime()));
		return calendar;
	}
	
	
	//距今天相对(dateLengTh)百分比 
	static public float fromTodayRate (Timestamp inputTime,int dateLength){
		long i=inputTime.getTime();
		long j=TimeTool.getCurrentTime().getTime();
		float r= (i-j)/(1000*60*60*24);
		if(r>dateLength)
			r=dateLength;
		if(r<0)
			r=0;
		return (float)(Math.round(r*100)/dateLength);
	}
	
	public enum ADD_TYPE {
        ONE_MONTH,THREE_MONTH,HALF_YEAR,ONE_YEAR
    }
	
	static public  Timestamp setTime(ADD_TYPE addType){
		Calendar calendar = Calendar.getInstance();  
		calendar.setTime(new Date());
		switch(addType){
		case ONE_MONTH:
			calendar.add(Calendar.MONTH, 1);
			break;
		case THREE_MONTH:
			calendar.add(Calendar.MONTH, 3);
			break;
		case HALF_YEAR:
			calendar.add(Calendar.MONTH, 6);
			break;
		case ONE_YEAR:
			calendar.add(Calendar.MONTH, 12);
			break;
		
		}
		return new Timestamp(calendar.getTimeInMillis());
		
	}
	
	public static void main(String[] args) throws IOException, InterruptedException {
		System.out.println(TimeTool.generateTime(2016, 5, 1));
		System.out.println(TimeTool.getMonthStartOrEnd(2));
		// TODO Auto-generated method stub
//		System.out.println(ClientTool.vagueName("刘德华"));
//		System.out.println(ClientTool.vaguePhone("15982076532"));
//		System.out.println(ClientTool.isMobileNO("111111111"));
//		System.out.println(TimeTool.getCurrentTime());
//		Thread.sleep(3000);
//		System.out.println(TimeTool.getCurrentTime());
		
//		Timestamp ts = new Timestamp(System.currentTimeMillis());
//		String tsStr = "2016-02-06 11:49:45";
//		try {
//			ts = Timestamp.valueOf(tsStr);
//			//System.out.println(ts);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
		
//		System.out.println(TimeTool.setTime(ADD_TYPE.ONE_YEAR));
////		System.out.println(TimeTool.fromTodayRate(ts,100));
//		System.out.println(System.currentTimeMillis());
//		System.out.println(TimeTool.getYestodayBegin());
//		System.out.println(TimeTool.nMonthLeater(6));
	}

}
